<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
    <script src="lib/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="lib/json2.js" type="text/javascript"></script>
    <script src="lib/jsmart.js" type="text/javascript"></script>
    <script src="script/app.js" type="text/javascript"></script>
    <script src="script/urledit.js" type="text/javascript"></script>
        
    <script id="suppliercount" type="text/x-jsmart-tmpl">
	{$hits.name}：共{$hits.total}条记录
    </script>
    <!--采集报价-->
    <script id="product" type="text/x-jsmart-tmpl">  
	   	<tr>
    	  	<th>编号</th>
           	<th>材料名称</th>
           	<th>规格</th>
          	<th>系列型号</th>
           	<th>品牌</th>
           	<th>单位</th>
           	<th>价格</th>
           	<th>更新时间</th>
			
        </tr>
		{foreach $hits.hits as $name => $supplier}
            <tr>
           		  	<td>{$supplier._source.productid}</td>
                	<td>{$supplier._source.name}</td>
                	<td>{$supplier._source.spec}</td>
                	<td>{$supplier._source.series}</td>
                	<td>{$supplier._source.brand}</td>
                	<td>{$supplier._source.unit}</td>
                	<td>{$supplier._source.price}</td>
                	<td>{$supplier._source.update_at}</td>
                </tr>
		{/foreach}
	</script>

    <script id="supplierpage" type="text/x-jsmart-tmpl">
	<div class="page">
		<div class="pagination">
			{if $pagecount > 1}			
				<a class="pageNum"><b>
				{$currentpage}
				</b>/<b>
				{$pagecount}
				</b></a>	
				{if (1 != $currentpage)}
					<a href="{urlset('pageIndex', 1)}" class="first">首页</a>
					<a href="{urlset('pageIndex', $currentpage-1)}" class="prev">上一页</a>
				{/if}	
				
				{for $i=$start to $end}
					{if ($i == $currentpage)}
						<span class="current">{$i}</span>
					{else}
						<a href="{urlset('pageIndex', $i)}">{$i}</a>
					{/if}
				{/for}
				
				{if ($pagecount != $currentpage)}
					<a href="{urlset('pageIndex', $currentpage+1)}" class="first">下一页</a>
					<a href="{urlset('pageIndex', $pagecount)}" class="last">尾页</a>
				{/if}	
			{/if}
		</div>
	</div>
	</script>
</head>
<body>
    <center>
        <div id="content">
            <style>
				table{border:1 solid;border-width 1 0 0 1;}
				#searchresult tr th{border:1 solid;}
    		</style>            
            <div id="count">
            </div>
        	<table cellpadding="10" cellspacing="0" border="1" id="table">
 
            </table>
      </div>      
      <div id="page">
      </div>
      <script type="text/javascript">
        $(document).ready(
			function () {								
				var myurl=new UrlEdit.URL(document.URL);
				var value = "*";
				var name="";
				var skip = 0;
				var pagesize = 10;
				if(myurl.get("pageIndex") != undefined)
					currentpage = myurl.get("pageIndex");
				if(myurl.get("pagesize") != undefined)
					pagesize = myurl.get("pagesize");
				if(myurl.get("id") != undefined && myurl.get("id") != "")
				{
					//values = myurl.get("searchvalue").split(' ');
					value = myurl.get("id");
				}
				if(myurl.get("name") != undefined && myurl.get("name") != "")
				{
					//values = myurl.get("searchvalue").split(' ');
					name = decodeURI(myurl.get("name"));
				}
				
					
				var skip = pagesize * (currentpage - 1);
				var pageparam = "?from=" + skip + "&size=" + pagesize;					
				//for (ind in values)
			//	{
				//	pageparam = pageparam + "&q=companyname:\"" + values[ind] + "\""
				//}
				var searchdb = "/material/product/_search";				
				var params = new Object();
                params["from"] = skip;
				params["size"] = pagesize;
				params["query"] = new Object();
				params["query"]["filtered"] = new Object();
				params["query"]["filtered"]["query"] = new Object();
				params["query"]["filtered"]["query"]["query_string"] = new Object();
				params["query"]["filtered"]["query"]["query_string"]["fields"] = new Array();
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("name");
				params["query"]["filtered"]["query"]["query_string"]["query"] =decodeURI("*");
				params["query"]["filtered"]["query"]["query_string"]["default_operator"] = "AND";
				params["query"]["filtered"]["filter"] = new Object();
				params["query"]["filtered"]["filter"]["and"] = new Array();
				
				var searchkey = new Object();
				searchkey["term"] = new Object();
				searchkey["term"]["supplierid"] = value;
				params["query"]["filtered"]["filter"]["and"].push(searchkey);
				
								
			    $.ajax({
			        url: searchpath + searchdb,
			        type: 'POST',	
                    dataType: 'json',
                    data: JSON.stringify(params),
			        success: function (data) {
						if(data.hits.total < 1)
						{
							alert("此供应商报价单为空！当页即将关闭");
							self.close();
						}
						var tpl = new jSmart(document.getElementById('product').innerHTML);

			            var res = tpl.fetch(data);
			            $("#table").html(res);
						
						page = new Array();
						page.currentpage = Number(currentpage);
						page.pagecount = Math.ceil(Number(data.hits.total) / Number(pagesize));
						page.start = 1;
						page.end = 9;
						if (Number(page.pagecount) > 9)
						{
							if (Number(currentpage) > 4)
							{
								if (Number(page.pagecount) - Number(currentpage) > 4)
								{
									page.start = Number(currentpage) - 4;
									page.end = Number(currentpage) + 4;
								}
								else
								{
									page.end = Number(page.pagecount);
									page.start = Number(page.end) - 8;
								}
							}
						
						}
						else
						{
							page.end = page.pagecount;
						}
						tpl = new jSmart(document.getElementById('supplierpage').innerHTML);
						res = tpl.fetch(page);
			            $("#page").html(res);			
						
						tpl = new jSmart(document.getElementById('suppliercount').innerHTML);
						data.hits.name = name;
			            var res = tpl.fetch(data);
			            $("#count").html(res);			
			        }
			    });
			}
		);
        </script>
    </center>
</body>
</html>
